<?php
class Content_AlbumController extends Areal_Controller_Action
{
    public function adminAction()
    {
        $model = new Content_Model_Album();
        $this->view->assign('albums', $model->getAll());
    }

    public function indexAction()
    {
        $album_id = $this->getRequest()->getParam('album_id');
        $album = Doctrine::getTable('Content_Model_Album')->find($album_id);
        $this->view->assign('album', $album);

    }

    public function showAction()
    {
        $page_id = $this->getRequest()->getParam('page_id');
        $model = Doctrine::getTable('Content_Model_Page')->find($page_id);
        $this->view->page = $model;
    }

    public function addAction()
    {
        $model = new Content_Model_Album();;
        if ($this->getRequest()->isPost()){
            if ($model->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Album saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','album','content',array());
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Album not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->form = $model->getEditingForm();
    }

    public function editAction( )
    {
        $album_id = $this->getRequest()->getParam('album_id');
        $album = Doctrine::getTable('Content_Model_Album')->find($album_id);
        $picture = new Content_Model_Picture();
        $uploadForm = new Default_Form_PictureUploader();
        if ($this->getRequest()->isPost()){
            $post = $this->getRequest()->getPost();
            $upload = $uploadForm->foo->getTransferAdapter();
            if ($upload->isUploaded()){
                $upload->receive();
                $filename =$upload->getFileName();

                if ($upload->isReceived() && ! empty($filename)){
                    $picture = new Content_Model_Picture();
                    $picture->filename = basename($filename);
                    $picture->album_id = $album->id;
                    $picture->save();
                    $file = DATA_PATH . 'content_pictures/picture-'.$picture->id.'.jpg';
                    unlink($file);
                    $filterFileRename = new Zend_Filter_File_Rename($file);
                    $filterFileRename->filter($filename);
                    return $this->_helper->redirector('edit','album','content', array('album_id'=>$album_id));

                }

            } elseif (isset($post['title'])){
                if ($album->edit($post)){
                    $message = Zend_Registry::get('Zend_Translate')->translate('Album saved');
                    $this->getHelper('flashMessenger')->addMessage($message);
                    return $this->_helper->redirector('admin','album','content');
                } else {
                    $message = Zend_Registry::get('Zend_Translate')->translate('Album not saved');
                    $this->getHelper('flashMessenger')->addMessage($message);
                }
            } 
        }
        $this->view->pictureUploadForm = $uploadForm;
        $this->view->form = $album->getEditingForm();

        $this->view->assign('pictures', $album->getPictures()->toArray());
    }

    public function deleteAction()
    {
        $album_id = $this->getRequest()->getParam('album_id');
        $album = Doctrine::getTable('Content_Model_Album')->find($album_id);
        $form = new Default_Form_ConfirmForm();
        if ($album && count($album->getPictures()) > 0){
            $message = Zend_Registry::get('Zend_Translate')->translate('In order to delete album please delete pictures from it first');
            $this->getHelper('flashMessenger')->addMessage($message);
            return $this->_helper->redirector('admin','album','content' );
        }
        if ($this->getRequest()->isPost()){
            if ($form->isValid($this->getRequest()->getPost())){
                $album->delete();
                $message = Zend_Registry::get('Zend_Translate')->translate('Album deleted');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','album','content' );
            }
        }
        $this->view->form = $form;
    }

    public function thumbAction()
    {header('Content-Type: image/jpeg');
        $picture_id = $this->getRequest()->getParam('picture_id');
        echo Content_Model_Picture::getThumb($picture_id);
        exit;
    }

    public function imageAction()
    {
        $picture_id = $this->getRequest()->getParam('picture_id');
        header('Content-Type: image/jpeg');
        echo Content_Model_Picture::getImage($picture_id);
        exit;
    }



}